using AutoFixture;
using AutoFixture.AutoMoq;
using Microsoft.Extensions.Configuration;
using Moq;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Terra.UnitTests.TestBase;

/// <summary>
/// 单元测试基类，提供通用的测试功能
/// </summary>
public abstract class UnitTestBase
{
    protected readonly IFixture Fixture;
    protected readonly IConfiguration Configuration;

    protected UnitTestBase()
    {
        // 配置AutoFixture与Moq集成
        Fixture = new Fixture()
            .Customize(new AutoMoqCustomization());

        // 获取测试配置
        Configuration = TestConfiguration.GetConfiguration();
    }

    /// <summary>
    /// 创建Mock对象
    /// </summary>
    protected Mock<T> CreateMock<T>() where T : class
    {
        return new Mock<T>();
    }

    /// <summary>
    /// 创建测试数据
    /// </summary>
    protected T Create<T>()
    {
        return Fixture.Create<T>();
    }

    /// <summary>
    /// 创建多个测试数据
    /// </summary>
    protected List<T> CreateMany<T>(int count = 3)
    {
        return Fixture.CreateMany<T>(count).ToList();
    }

    /// <summary>
    /// 获取配置项值
    /// </summary>
    protected string GetConfiguration(string key)
    {
        return Configuration[key];
    }

    /// <summary>
    /// 获取类型化配置项
    /// </summary>
    protected T GetConfiguration<T>(string key) where T : class
    {
        return Configuration.GetSection(key).Get<T>();
    }
}